package com.cqsd;

import com.cqsd.ioc.annotation.Autowrite;
import com.cqsd.ioc.annotation.Component;
import com.cqsd.ioc.trait.use.BeanNameAware;
import com.cqsd.ioc.trait.use.BeanRun;
import com.cqsd.ioc.trait.use.DestructFunction;

import java.util.concurrent.TimeUnit;

/**
 * @author caseycheng
 * @date 2023/3/13-04:31
 **/
@Component
public class UserService implements BeanNameAware, DestructFunction, BeanRun {
    private Service service;

    @Override
    public void destruct() {
        service=null;
        System.out.println("我是userService我被回收了");
    }

    @Autowrite(ServiceImpl.class)
    public UserService(Service service) {
        this.service = service;
        service.say();
    }


    @Override
    public void setName(String name) {
        System.out.println("userService 被分配了" + name);
    }

    @Override
    public Runnable run() {
        System.out.println("我是userServiec我要睡10秒");
        for (int i=1;i<=10;i++){
            try {
                TimeUnit.SECONDS.sleep(1);
                System.out.printf("userService:%d\r",i);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        System.out.println("我是userService我起床了");
        return null;
    }
}
